﻿using System;
using System.Collections.Generic;
using System.Text;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Entities;
using System.Data.SqlClient;
using System.Data;

namespace NIIT.SaoBacDau.Aupi.HospitalManagement.DataAccess
{
    public class DAO_HR_Employee
    {
        public static int UpdateEmployee(HR_Employee hr_Em)
        {
            SqlConnection SqlCon = DAO_ConnectDatabase.Connect();
            try
            {
                SqlCon.Open();
                SqlCommand sqlcom = new SqlCommand("[HumanResources].[UpdateEmployee]", SqlCon);
                sqlcom.CommandType = CommandType.StoredProcedure;
                sqlcom.Parameters.Add("@Address", SqlDbType.NVarChar).Value = hr_Em.Address;
                sqlcom.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = hr_Em.PhoneNumber;
                sqlcom.Parameters.Add("@Email", SqlDbType.NVarChar).Value = hr_Em.Email;
                sqlcom.Parameters.Add("@EmployeeID ", SqlDbType.Int).Value = hr_Em.EmployeeID;
                return sqlcom.ExecuteNonQuery();
            }
            finally
            {
                if (SqlCon.State == ConnectionState.Open)
                    SqlCon.Close();
            }
        }

        public static HR_Employee Employee(HR_Employee hr_Em)
        {
            SqlDataAdapter sqlda = new SqlDataAdapter("[HumanResources].[HrEmployee]", DAO_ConnectDatabase.Connect());
            sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
            sqlda.SelectCommand.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = hr_Em.EmployeeID;
            DataTable dtable = new DataTable();
            sqlda.Fill(dtable);
            HR_Employee hr_Employee = new HR_Employee();
            hr_Employee.EmployeeID = hr_Em.EmployeeID;
            hr_Employee.NameEmployee = dtable.Rows[0]["NameEmployee"].ToString();
            hr_Employee.Address = dtable.Rows[0]["Address"].ToString();
            hr_Employee.BirthDay = dtable.Rows[0]["BirthDay"].ToString();
            hr_Employee.Gender = Convert.ToBoolean(dtable.Rows[0]["Gender"].ToString());
            hr_Employee.Identification = Convert.ToInt32(dtable.Rows[0]["Identification"].ToString());
            hr_Employee.PhoneNumber = dtable.Rows[0]["PhoneNumber"].ToString();
            hr_Employee.Email = dtable.Rows[0]["Email"].ToString();
            hr_Employee.Salary = Convert.ToDouble(dtable.Rows[0]["Salary"].ToString());
            hr_Employee.StartDate = dtable.Rows[0]["StartDate"].ToString();
            hr_Employee.EndDate = dtable.Rows[0]["EndDate"].ToString();
            hr_Employee.TypeEmployeesID = Convert.ToInt32(dtable.Rows[0]["TypeEmployeesID"].ToString());
            hr_Employee.PositionsID = Convert.ToInt32(dtable.Rows[0]["PositionsID"].ToString());
            hr_Employee.OrganizationDetailsID = Convert.ToInt32(dtable.Rows[0]["OrganizationDetailsID"].ToString());
            return hr_Employee;
        }
    }
}
